package com.example.wastewater.service.impl;

import com.example.wastewater.common.convention.exception.ClientException;
import com.example.wastewater.service.IEmailService;
import com.example.wastewater.toolkit.EmailValidator;
import lombok.RequiredArgsConstructor;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class EmailServiceImpl implements IEmailService {

    private final JavaMailSender mailSender;

    public void sendEmail(String to, String subject, String text) {
        EmailValidator emailValidator = new EmailValidator();
        if(!emailValidator.validate(to)){
            throw new ClientException("邮箱格式不合法");
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        message.setFrom("18975711158@163.com");

        mailSender.send(message);
    }

}
